﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using com.tiyma.components;

namespace com.tiyma.service
{
    public partial class DataSynchService : ServiceBase
    {


        System.Threading.Thread m_upload_thd = null;
        System.Threading.Thread m_import_thd = null;

        public DataSynchService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            LogHelper.Info(string.Format("UpLoadInterval={0},ImportInterval={1}", AppConfig.UpLoadInterval, AppConfig.ImportInterval));
            try
            {
                if (AppConfig.UpLoadInterval != 0)
                {
                    FileUploadManager t_file_upload_mgr = new FileUploadManager();
                    m_upload_thd = new System.Threading.Thread(t_file_upload_mgr.UploadFileByService);
                    if (m_upload_thd != null)
                    {
                        m_upload_thd.Start();
                        LogHelper.Info("启动上传文件线程!");
                    }
                }

                if (AppConfig.ImportInterval != 0)
                {
                    BCPManager t_bcp_mgr = new BCPManager();
                    m_import_thd = new System.Threading.Thread(t_bcp_mgr.ImportDataByService);
                    if (m_import_thd != null)
                    {
                        m_import_thd.Start();
                        LogHelper.Info("启动文件导入到数据库线程!");
                    }

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("启动服务失败" + ex.ToString());
            }
        }

      

        protected override void OnStop()
        {
            try
            {
                if (m_upload_thd != null)
                {
                    m_import_thd.Abort();
                    LogHelper.Info("上传文件线程停止..");
                }

                if (m_import_thd != null)
                {   
                    m_import_thd.Abort();
                    LogHelper.Info("导入数据线程停止...");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("停止服务关闭线程失败" + ex.ToString());
            }
        }
    }
}
